﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BSC_OTC_Management.Forms
{
    public partial class Frm_EditPermission : Form
    {

        #region khai bao bien
        Code.StaticVariable _code = new Code.StaticVariable();
        string _autoid = "";
        DataTable _tablePermission = new DataTable();
        #endregion

        public Frm_EditPermission(string autoid)
        {
            this._autoid = autoid;
            InitializeComponent();
        }

        private void FrmEditPermission_Load(object sender, EventArgs e)
        {
            LoadData();
            ShowChecked();
        }

        private void ShowChecked()
        {
            char[] authcode = this._tablePermission.Rows[0]["authcode"].ToString().ToCharArray();
            for (int i = 0; i < authcode.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        if (authcode[i] == 'Y')
                        {
                            this.cbThemMoi.Checked = true;
                        }
                        break;
                    case 1:
                        if (authcode[i] == 'Y')
                        {
                            this.cbXem.Checked = true;
                        }
                        break;
                    case 2:
                        if (authcode[i] == 'Y')
                        {
                            this.cbSua.Checked = true;
                        }
                        break;
                    case 3:
                        if (authcode[i] == 'Y')
                        {
                            this.cbXoa.Checked = true;
                        }
                        break;
                    case 4:
                        if (authcode[i] == 'Y')
                        {
                            this.cbTimKiem.Checked = true;
                        }
                        break;
                    case 5:
                        if (authcode[i] == 'Y')
                        {
                            this.cbKetXuat.Checked = true;
                        }
                        break;
                    case 6:
                        if (authcode[i] == 'Y')
                        {
                            this.cbbDuyet.Checked = true;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        private void LoadData()
        {
            _tablePermission = _code._services.Get_Permission_ByAutoID(this._autoid);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string authcode = "";
            if (this.cbThemMoi.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbXem.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbSua.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbXoa.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbTimKiem.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbKetXuat.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            if (this.cbbDuyet.Checked)
            {
                authcode += "Y";
            }
            else
            {
                authcode += "N";
            }

            DataTable _tableValue = new DataTable("noname");
            _tableValue.Columns.Add("grpid");
            _tableValue.Columns.Add("menuid");
            _tableValue.Columns.Add("authcode");

            DataRow _row = _tableValue.NewRow();
            _row["grpid"] = this._tablePermission.Rows[0]["grpid"].ToString();
            _row["menuid"] = this._tablePermission.Rows[0]["menuid"].ToString();
            _row["authcode"] = authcode;
            _tableValue.Rows.Add(_row);

            DataRow _rowDataType = _tableValue.NewRow();
            _rowDataType["grpid"] = "T";
            _rowDataType["menuid"] = "T";
            _rowDataType["authcode"] = "T";
            _tableValue.Rows.Add(_rowDataType);
            try
            {
                _code._services.EditRow("bsc_group_permission", _tableValue, "autoid", this._autoid, Code.StaticVariable.userID);
                MessageBox.Show(Code.StaticVariable.messeseboxSuaThanhCong);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);       
            }
        }
    }
}